package com.idlecode.manage;

import com.idlecode.bean.DataSourceEntity;
import com.idlecode.panel.DataSourcesPanel;
import com.idlecode.service.DataSourceService;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import java.util.List;

public class DataSourceFormManage extends TitleAreaDialog {
	private Text connectionNameField;
	private Combo databaseTypeComboBox;
	private Text databaseUrlField;
	private Text userNameField;
	private Text passwordField;
	private Integer indexOf;
	private String connectionName;
	private String databaseType;
	private String databaseUrl;
	private String userName;
	private String password;
	private static final String[] databaseTypes = new String[] { "MySQL", "MariaDB", "Oracle", "PostgreSQL",
			"SQLServer", "DM", "Clickhouse" };
	private DataSourcesPanel dataSourcesPanel;

	public DataSourceFormManage(Shell parentShell, Integer indexOf, String connectionName, String databaseType,
			String databaseUrl, String userName, String password, DataSourcesPanel dataSourcesPanel) {
		super(parentShell);
		this.indexOf = indexOf;
		this.connectionName = connectionName;
		this.databaseType = databaseType;
		this.databaseUrl = databaseUrl;
		this.userName = userName;
		this.password = password;
		this.dataSourcesPanel = dataSourcesPanel;
	}

	public DataSourceFormManage(Shell parentShell, DataSourcesPanel dataSourcesPanel) {
		super(parentShell);
		this.dataSourcesPanel = dataSourcesPanel;
	}

	@Override
	public void create() {
		super.create();
		setTitle("数据源设置");
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(1, false)); // 设置 container 的布局

		// 创建新的 Composite
		Composite dataBaseComposite = new Composite(container, SWT.NONE);
		dataBaseComposite.setLayout(new GridLayout(2, false));
		dataBaseComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		createLabel(dataBaseComposite, "连接名:");
		connectionNameField = new Text(dataBaseComposite, SWT.BORDER);
		connectionNameField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		if (connectionName != null) {
			connectionNameField.setText(connectionName);
		}

		createLabel(dataBaseComposite, "数据库类型:");
		databaseTypeComboBox = new Combo(dataBaseComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
		databaseTypeComboBox.setItems(databaseTypes);
		databaseTypeComboBox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		if (databaseType != null) {
			databaseTypeComboBox.setText(databaseType);
		}

		createLabel(dataBaseComposite, "数据库URL:");
		databaseUrlField = new Text(dataBaseComposite, SWT.BORDER);
		databaseUrlField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		if (databaseUrl != null) {
			databaseUrlField.setText(databaseUrl);
		}

		createLabel(dataBaseComposite, "用户名:");
		userNameField = new Text(dataBaseComposite, SWT.BORDER);
		userNameField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		if (userName != null) {
			userNameField.setText(userName);
		}

		createLabel(dataBaseComposite, "密码:");
		passwordField = new Text(dataBaseComposite, SWT.BORDER | SWT.PASSWORD);
		passwordField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		if (password != null) {
			passwordField.setText(password);
		}

		return container;
	}

	private void createLabel(Composite container, String text) {
		Label label = new Label(container, SWT.NONE);
		label.setText(text);
		GridData gridData = new GridData();
		label.setLayoutData(gridData);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(500, 350); // 设置对话框的初始大小
	}

	@Override
	protected void okPressed() {
		saveConnectionSettings();
	}

	private void saveConnectionSettings() {
		connectionName = connectionNameField.getText();
		if (connectionName.isEmpty()) {
			MessageDialog.openWarning(getShell(), "警告", "连接名不能为空！");
			return;
		}
		databaseType = databaseTypeComboBox.getText();
		if (databaseType.isEmpty()) {
			MessageDialog.openWarning(getShell(), "警告", "数据库类型不能为空！");
			return;
		}
		databaseUrl = databaseUrlField.getText();
		if (databaseUrl.isEmpty()) {
			MessageDialog.openWarning(getShell(), "警告", "数据库URL不能为空！");
			return;
		}
		userName = userNameField.getText();
		if (userName.isEmpty()) {
			MessageDialog.openWarning(getShell(), "警告", "用户名不能为空！");
			return;
		}
		password = passwordField.getText();
		if (password.isEmpty()) {
			MessageDialog.openWarning(getShell(), "警告", "密码不能为空！");
			return;
		}

		List<DataSourceEntity> dataSourcesRows = DataSourceService.getDataSources();
		DataSourceEntity dataSourceEntity = new DataSourceEntity(databaseType, connectionName, databaseUrl, userName,
				password);
		if (indexOf == null) {
			dataSourcesRows.add(dataSourceEntity);
		} else {
			dataSourcesRows.set(indexOf, dataSourceEntity);
		}
		DataSourceService.setDataSources(dataSourcesRows);
		dataSourcesPanel.queryBtnAction();
		super.okPressed();
	}
}
